import store from '../store/index.js'

const siteinfo = process.env.NODE_ENV==='development' ? require('@/siteinfo.js') : __non_webpack_require__('../siteinfo.js')

// const siteinfo=__non_webpack_require__('../siteinfo.js')
// const base = siteinfo.siteroot + '?i=' + siteinfo.uniacid + '&a=wxapp&c=entry&m=lingchi_blind_box&do=api&s='

// 测试
const base = siteinfo.siteroot + '?i=' + siteinfo.uniacid + '&a=wxapp&c=entry&m=lingchi_guess_box&do=api&s='
const url = 'http://liang.nat200.top'
// const base = siteinfo.siteroot



// 基于uni.request()的请求封装
const request = (url, data, showLoading=true, token=store.getters.getToken || '') => {
	if(showLoading) {
		uni.showLoading({
			title: '加载中...',
			mask: true
		})
	}
	
	
	let DefaultOpts = {
		url: url,
		data: data,
		method: 'POST',
		header: {
			'token': token,
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
			"Content-Type": "application/json; charset=UTF-8"
		},
		dataType: 'json'
	}
	
	return new Promise(function(resolve, reject) {
		uni.request(DefaultOpts)
		.then((res) => {
				uni.hideLoading()
				const {data} = res[1]
				
				if(data.code===200) {
					resolve(data)
				} else {
					if(data.code===401) {
						store.commit('setToken', '');
						uni.clearStorageSync();
					}
					if(data.code!==50002){
						uni.showToast({
						    title: data.msg,
							icon: 'none',
							mask: true,
						    duration: 3000
						});
					}
					
					
					resolve(data)
				}
				
			}
		).catch(
			(response) => {
				uni.hideLoading()
				reject(response)
			}
		)
	})
}


//带Token请求
const req = (uri, data, showLoading=true) => {
	if(showLoading) {
		uni.showLoading({
			title: '处理中...',
			mask: true
		})
	}
	
	// let token = store.getters.getToken;
	let token = uni.getStorageSync('token') || ''
	let url = base + uri;
	
	//此token是登录成功后后台返回保存在storage中的
	let DefaultOpts = {
		url: url,
		data: data,
		method: 'POST',
		header: {
			'token': token,
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
			"Content-Type": "application/json; charset=UTF-8"
		},
		dataType: 'json',
	}
	
	if (!token) {
		return new Promise(function(resolve, reject){
			uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					return request(base + '/login', {code: loginRes.code}, showLoading)
					.then((loginRes) => {
						store.commit('setUser', loginRes.data.userinfo);
						store.commit('setToken', loginRes.data.token);
						
						return request(base + uri, data, showLoading, loginRes.data.token).then((apiRes) => {
							
							resolve(apiRes)
						})
					})
				}
			});
		})
		
	} else {
		return request(base + uri, data, showLoading)
	}

}


// 图片上传
const file = (maxCount) => {
	let url = base + '/upload/image';
	//此token是登录成功后后台返回保存在storage中的
	maxCount = maxCount || 1
	let promise = new Promise(function(resolve, reject) {
		uni.chooseImage({
			count: maxCount,
			sizeType: 'original',
			success: (chooseImageRes) => {
				const tempFilePaths = chooseImageRes.tempFilePaths;
				uni.showLoading({
					title: '上传中...'
				})
				
				uni.uploadFile({
					url: url,
					filePath: tempFilePaths[0],
					name: 'file',
					success: (uploadFileRes) => {
						uni.hideLoading()
						resolve(JSON.parse(uploadFileRes.data))
					},
					fail: (response) => {
						uni.hideLoading()
						reject(response)
					}
				});
			}
		});
	})
	return promise
}
export default {
	req,
	file,
	base,
	url
}
